//
//  Tool.m
//  Lmm_IOS
//
//  Created by 张哲炜 on 2018/9/20.
//  Copyright © 2018年 lmm. All rights reserved.
//

#import "Tool.h"

@implementation Tool

+ (BOOL)isMobile:(NSString *) mobile {
    NSString *mobileRegex = @"^(1(([2345789][0-9])))\\d{8}$";
    //NSString *mobileRegex = @"^((13[0-9])|(15[3-5])|(18[07-9]))\\d{8}$";
    NSPredicate *mobileTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobileRegex];
    return [mobileTest evaluateWithObject:mobile];
}

+ (void)showLoadingView:(NSString *)msg {
    
    if (msg.length == 0) {
        [SVProgressHUD show];
    } else {
        [SVProgressHUD showWithStatus:msg];
    }
    
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeNative];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
}

+ (void)dismissLoadingView {
    [SVProgressHUD dismiss];
}

+ (void)showHUD:(NSString *)msg :(UIView *)rootView {
    
    
//    JGProgressHUD *HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
//    HUD.textLabel.text =  msg;
//    [HUD showInView:rootView];
//    [HUD dismissAfterDelay:3.0];
    
    JGProgressHUD *HUD = [[JGProgressHUD alloc] initWithStyle:JGProgressHUDStyleDark];
    
    JGProgressHUDFadeZoomAnimation *an = [JGProgressHUDFadeZoomAnimation animation];
    HUD.animation = an;
    HUD.indicatorView = nil;
    HUD.textLabel.text = msg;
    HUD.position = JGProgressHUDPositionBottomCenter;
    HUD.layoutMargins = (UIEdgeInsets) {
        .top = 0.0f,
        .bottom = MAIN_SCREEN_HEIGHT/2,
        .left = 0.0f,
        .right = 0.0f,
    };
    
    [HUD showInView:rootView];
    [HUD dismissAfterDelay:1.0];
}


+ (void)showErrorHUD {
    JGProgressHUD *HUD = [[JGProgressHUD alloc] initWithStyle:JGProgressHUDStyleDark];
    
    HUD.textLabel.text = @"Error!";
    HUD.indicatorView = [[JGProgressHUDErrorIndicatorView alloc] init];
    
    HUD.square = YES;
    
    [HUD showInView:[[AppDelegate shareAppDelegate] shareNavigationController].view];
    
    [HUD dismissAfterDelay:3.0];
}

+ (NSString *)getFormatDate:(int64_t)time {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    
    NSDateFormatter *dateFormatter = NSDateFormatter.new;
    [dateFormatter setDateFormat:@"MM-dd HH:mm"];
    NSString *dateStr = [dateFormatter stringFromDate:date];
    
    NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:date];
    int days = ((int)timeInterval)/(3600*24);
    int hours = ((int)timeInterval)%(3600*24)/3600;
    int mins = ((int)timeInterval)%(3600*24)%3600/60;
    NSString *formatDate = NSString.new;
    if (days == 0 && hours == 0 && mins == 0) {
        formatDate = [formatDate stringByAppendingString:@"刚刚"];
    } else if (days == 0 && hours == 0 && mins <= 59) {
        formatDate = [formatDate stringByAppendingString:[NSString stringWithFormat:@"%d分钟前",mins]];
    } else if(days == 0 && hours <= 23) {
        formatDate = [formatDate stringByAppendingString:[NSString stringWithFormat:@"%d小时前",hours]];
    }else {
        formatDate = dateStr;
    }
    return formatDate;
}

@end
